package com.bicon.botu.mall.base.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.mall.base.common.MallHomePageConstants;
import com.bicon.botu.mall.base.entity.HomeIcon;
import com.bicon.botu.mall.base.mapper.HomeIconMapper;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.JsonUtil;
import com.bicon.botu.core.base.repository.redis.RedisHandler;
import com.bicon.botu.mall.base.service.IHomeIconService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页图标 服务实现类
 *
 * @author 张凡
 * @date 2017/7/19 下午 14:49
 */
@Service
@Transactional
public class HomeIconServiceImpl extends ServiceImpl<HomeIconMapper, HomeIcon> implements IHomeIconService {
    @Autowired
    RedisHandler redisHandler;
    private static final String REDIS_KEY = MallHomePageConstants.BOTU + MallHomePageConstants.REDIS_KEY_DELIMITER + MallHomePageConstants.BASE +
            MallHomePageConstants.REDIS_KEY_DELIMITER + MallHomePageConstants.HOME_ICON;

    /**
     * 新增图标
     *
     * @param homeIcons
     * @return
     */
    @Override
    public boolean insert(List<HomeIcon> homeIcons) {
        for (HomeIcon homeIcon : homeIcons) {
            homeIcon.preInsert();
            Boolean result = homeIcon.insert();
            if (!result) {
                return false;
            }
        }
        this.initHomeIconToRedis();
        return true;
    }

    /**
     * 逻辑删除图标
     *
     * @param idJson 图标id集合
     * @return
     */
    @Override
    public BaseResult delete(String idJson) {
        ArrayList<String> idList = JsonUtil.fromJson(idJson, ArrayList.class);
        List<HomeIcon> homeIconList = new ArrayList<>();
        for (int i = 0; i < idList.size(); i++) {
            HomeIcon homeIcon = new HomeIcon();
            homeIcon.setLogicDelete(Constants.DEL_LOGIC_DELETE);
            homeIcon.preUpdate();
            homeIconList.add(homeIcon);
        }
        boolean result = this.updateBatchById(homeIconList);
        if (!result) {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
        //删除后重新初始化
        this.initHomeIconToRedis();
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 修改图标
     *
     * @param id         图标id
     * @param grayIcon   灰色图标
     * @param brightIcon 高亮图标
     * @param iconFlag   图标开关:0.开，1.关
     * @return
     */
    @Override
    public BaseResult update(Integer id, String grayIcon, String brightIcon, Integer iconFlag) {
        HomeIcon homeIcon = new HomeIcon().selectOne(new EntityWrapper<>().eq("id", id)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (null == homeIcon) {
            return new BaseResult(Constants.RESPONSE_CODE_404, Constants.RESPONSE_MESSAGE_404);
        }
        homeIcon.preUpdate();
        if (null != brightIcon) {
            homeIcon.setBrightIcon(brightIcon);
        }
        if (null != grayIcon) {
            homeIcon.setGrayIcon(grayIcon);
        }
        if (null != iconFlag) {
            homeIcon.setIconFlag(iconFlag);
        }
        boolean result = homeIcon.updateById();
        if (!result) {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500);
        }
        //将频道存到redis
        this.initHomeIconToRedis();
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
    }

    /**
     * 初始化所有图标到redis
     *
     * @return
     */
    @Override
    public BaseResult initHomeIconToRedis() {
        //清除缓存
        redisHandler.getLocalRedisTemplate().remove(REDIS_KEY, "");
        //查询出所有的正常状态的所有首页图标
        List<HomeIcon> homeIcons = new HomeIcon().selectList(new EntityWrapper<HomeIcon>()
                .eq("logic_delete", MallHomePageConstants.DEL_LOGIC_NORMAL).eq("icon_flag", MallHomePageConstants.ON));
        List<String> stringList = new ArrayList<>();
        for (HomeIcon homeIcon : homeIcons) {
            stringList.add(JsonUtil.toJson(homeIcon));
        }
        //保存到redis上
        redisHandler.getLocalRedisTemplate().cacheList(REDIS_KEY, "", stringList);
        return new BaseResult(MallHomePageConstants.RESPONSE_CODE_200, MallHomePageConstants.RESPONSE_MESSAGE_200);
    }

    /**
     * 获取首页图标详情
     *
     * @return
     */
    @Override
    public List<HomeIcon> getHomeIconDetailFromRedis() {
        //从redis上获取全部图标详情
        List<String> list = redisHandler.getLocalRedisTemplate().getList(REDIS_KEY, "");
        List<HomeIcon> homeIcons = new ArrayList<>();
        for (String str : list) {
            HomeIcon homeIcon = JsonUtil.fromJson(str, HomeIcon.class);
            homeIcons.add(homeIcon);
        }
        return homeIcons;
    }
}
